package com.ljyh.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljyh.forum.dto.StatisticsDTO;
import com.ljyh.forum.service.CommentService;
import com.ljyh.forum.service.PostService;
import com.ljyh.forum.service.StatisticsService;
import com.ljyh.forum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 统计服务实现类
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    @Override
    public StatisticsDTO getStatistics() {
        StatisticsDTO statistics = new StatisticsDTO();
        
        // 获取今日的开始和结束时间
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        
        // 今日发帖数
        QueryWrapper<com.ljyh.forum.entity.Post> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.between("create_time", todayStart, todayEnd);
        statistics.setTodayPosts(postService.count(postQueryWrapper));
        
        // 总贴数
        statistics.setTotalPosts(postService.count());
        
        // 总用户数
        statistics.setTotalUsers(userService.count());
        
        // 今日新增用户数
        QueryWrapper<com.ljyh.forum.entity.User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.between("create_time", todayStart, todayEnd);
        statistics.setTodayNewUsers(userService.count(userQueryWrapper));
        
        // 总评论数
        statistics.setTotalComments(commentService.count());
        
        // 今日新增评论数
        QueryWrapper<com.ljyh.forum.entity.Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.between("create_time", todayStart, todayEnd);
        statistics.setTodayComments(commentService.count(commentQueryWrapper));
        
        return statistics;
    }
}